
function clone(target) {  // 考虑数组的版本
    if (typeof target === 'object') {
        let cloneTarget = Array.isArray(target) ? [] : {}; // 进行判断，判断是克隆的是否为数组
        for (const key in target) {
            cloneTarget[key] = clone(target[key]);
        }
        return cloneTarget;
    } else {
        return target;
    }
};

// ----------------------- 测试



const target = {
    field1: 1,
    field2: undefined,
    field3: {
        child: 'child'
    },
    field4: [2, 4, 8]
};

const result = clone(target);

result.field4[0] = 99;

console.log(result);
console.log(target, "-----");